import 'package:dio/dio.dart';
import 'package:sp_util/sp_util.dart';
import '../res/constant.dart';
import 'intercept.dart';

HttpConfig httpConfig = HttpConfig._();

const String SERVER_TYPE_RELEASE = "release";
const String SERVER_TYPE_DEV = "dev";
const String SERVER_TYPE_TEST = "test";

class HttpConfig {
  //默认正式环境
  static const String BASE_URL_BLUESTAR = "https://pccappapi.jiazhengye.cn";
  static const String BASE_URL_M = "https://m2.jiazhengye.cn";
  static const String BASE_URL_M2 = "https://m2.jiazhengye.cn";

  //测试环境
  static const String TEST_BASE_URL = "https://pccappapi-test.jiazhengye.cn";
  static const String TEST_BASE_URL_M = "https://pccm-dev.jiazhengye.cn";
  static const String TEST_BASE_URL_M2 = "https://pccm-dev.jiazhengye.cn";

  //开发环境
  static const String DEV_BASE_URL = "https://pccappapi-dev.jiazhengye.cn";
  static const String DEV_BASE_URL_M = "https://m2-dev.jiazhengye.cn";
  static const String DEV_BASE_URL_M2 = "https://m2-dev.jiazhengye.cn";

  static const int _TIME_OUT_DURATION = 15000;

  var _xmjztoken = "03734b178f10c34091d471938eb979ab";
  var _xmjzversion = "1.0.0";
  var _project_uuid = "01a24c4d2a858cf754ae0529a71d732d"; //a3e0c3d95c96785a6b80f0a435c8a468 这是群英科技园的
  var _xmjzdevice = "vivo v2057A"; //手机机型 xmjzdevice

  String get token => _xmjztoken;

  String get version => _xmjzversion;

  String get device => _xmjzdevice;

  String get project_uuid => _project_uuid;

  set serverType(value) {
    _serverType = value;
  }

  /// 打包切换环境修改此参数即可，无需频繁修改服务器地址
  var _serverType = SpUtil.getString(Constant.netEnvironment, defValue: SERVER_TYPE_RELEASE) ?? SERVER_TYPE_RELEASE;
  // var _serverType = SpUtil.getString(Constant.netEnvironment, defValue: SERVER_TYPE_DEV) ?? SERVER_TYPE_DEV;

  String get serverType => _serverType;

  Dio? _dio;
  Dio? _mDio;
  Dio? _m2Dio;

  HttpConfig._();

  final _options = BaseOptions(
    baseUrl: DEV_BASE_URL,
    headers: {
      "xmjzplatform": "Android",
    },
    connectTimeout: _TIME_OUT_DURATION,
    receiveTimeout: _TIME_OUT_DURATION,
    sendTimeout: _TIME_OUT_DURATION,
  );

  final _mOptions = BaseOptions(
    baseUrl: DEV_BASE_URL_M,
    contentType: "application/x-www-form-urlencoded",
    headers: {
      "xmjzplatform": "Android",
    },
    connectTimeout: _TIME_OUT_DURATION,
    receiveTimeout: _TIME_OUT_DURATION,
    sendTimeout: _TIME_OUT_DURATION,
  );

  final _m2Options = BaseOptions(
    baseUrl: DEV_BASE_URL_M2,
    contentType: "application/x-www-form-urlencoded",
    headers: {
      "xmjzplatform": "Android",
    },
    connectTimeout: _TIME_OUT_DURATION,
    receiveTimeout: _TIME_OUT_DURATION,
    sendTimeout: _TIME_OUT_DURATION,
  );

  ///*************************************************************************
  ///设置请求参数及环境参数
  void initHttpParams(Map<String, dynamic>? params) {
    print("跳转接受的参数 $params");
    Constant.inProduction = false;
    if (params == null || params.isEmpty || params["serverType"] == null) {
      return;
    }
    //这里赋值一下项目的 uuid
    if (params["project_uuid"] != null) {
      _project_uuid = params["project_uuid"];
    }

    //这里赋值一下token
    if (params["xmjztoken"] != null) {
      _xmjztoken = params["xmjztoken"];
    }

    //这里赋值一下version
    if (params["xmjzversion"] != null) {
      _xmjzversion = params["xmjzversion"];
    }

    //这里赋值一下机型
    if (params["xmjzdevice"] != null) {
      _xmjzdevice = params["xmjzdevice"];
    }

    //判断是什么环境
    if (_serverType != params["serverType"]!) {
      _serverType = params["serverType"]!;
    }

    if (_serverType == SERVER_TYPE_RELEASE) {
      Constant.inProduction = true;
    } else {
      Constant.inProduction = false;
    }
  }

  ///*************************************************************************
  /// 创建dio实例，并根据不同环境进行配置
  Dio get dio => getDio();

  Dio getDio({String contentType = "application/x-www-form-urlencoded"}) {
    if (_dio == null) {
      _dio = Dio(_options);
      _addIntercepter(_dio!);
    }
    _dio!.options.contentType = contentType;

    if (_serverType == SERVER_TYPE_RELEASE) {
      _dio!.options.baseUrl = BASE_URL_BLUESTAR;
    } else if (_serverType == SERVER_TYPE_TEST) {
      _dio!.options.baseUrl = TEST_BASE_URL;
    } else {
      _dio!.options.baseUrl = DEV_BASE_URL;
    }

    return _dio!;
  }

  Dio get mDio => _getMdio();

  Dio _getMdio() {
    if (_mDio == null) {
      _mDio = Dio(_mOptions);
      _addIntercepter(_mDio!);
    }
    if (_serverType == SERVER_TYPE_RELEASE) {
      _mDio!.options.baseUrl = BASE_URL_M;
    } else if (_serverType == SERVER_TYPE_TEST) {
      _mDio!.options.baseUrl = TEST_BASE_URL_M;
    } else {
      _mDio!.options.baseUrl = DEV_BASE_URL_M;
    }
    return _mDio!;
  }

  Dio get m2Dio => _getM2dio();

  Dio _getM2dio() {
    if (_m2Dio == null) {
      _m2Dio = Dio(_m2Options);
      _addIntercepter(_m2Dio!);
    }
    if (_serverType == SERVER_TYPE_RELEASE) {
      _m2Dio!.options.baseUrl = BASE_URL_M2;
    } else if (_serverType == SERVER_TYPE_TEST) {
      _m2Dio!.options.baseUrl = TEST_BASE_URL_M2;
    } else {
      _m2Dio!.options.baseUrl = DEV_BASE_URL_M2;
    }
    return _m2Dio!;
  }

  ///*************************************************************************
  /// 请求拦截器，为请求添加统一参数
  _addIntercepter(Dio dio) {
    /// 签名拦截器
    dio.interceptors.add(AuthInterceptor());

    dio.interceptors.add(SignInterceptor());

    /// 打印Log(生产模式去除)
    // if (!Constant.inProduction) {
    dio.interceptors.add(LoggingInterceptor());
    // }
    /// 适配数据(根据自己的数据结构，可自行选择添加)
    dio.interceptors.add(AdapterInterceptor());
  }

  /// 判断服务器环境类型
  bool isReleaseServer() {
    return _serverType == SERVER_TYPE_RELEASE;
  }

  bool isTestServer() {
    return _serverType == SERVER_TYPE_TEST;
  }

  bool isDevServer() {
    return _serverType == SERVER_TYPE_DEV;
  }
}
